library(foreign)
library(doBy)
library(dplyr)
#Set random number generator values
set.seed(123123)
##Read data
#Set directory
setwd("~/OneDrive - Indiana University/FromGoogle/DiseaseFood/AJARESubmissionCode_4_24/")

##Read chemical series
chem.fert <- read.csv("FAOchemfull.csv")
summary(chem.fert)
#Collapse all chemicals
chem.fert.s <- summaryBy(Value~Year, data=chem.fert, keep.names = T, FUN=c("sum"))
names(chem.fert.s) <- c("year", "chem_fert")
chem.fert.s

##Read manure series
man.fert <- read.csv("FAOmanurefull.csv")
summary(man.fert)
#Collapse all chemicals
man.fert.s <- summaryBy(Value~Year, data=man.fert, keep.names = T, FUN=c("sum"))
man.fert.s
#Change names
names(man.fert.s) <- c("year", "man_fert") 
###Join series together 
fert.comb <- left_join(chem.fert.s,man.fert.s)
fert.comb

###Plot chemical series
p1 <- ggplot(data = fert.comb, 
             aes(x = year, y = log(chem_fert) ))+
  geom_line(color = "lightblue", size = 1)+
  # geom_smooth(color = "#00AFBB", size = 1)+
  ggtitle( "Chemical fertilizer imports")+ 
  theme(plot.title = element_text(size=12,hjust = 0.5,face="bold"))+ 
  theme(axis.title.x=element_text(face="bold"),
        axis.text.x=element_text(color="black", 
                                 size=8, angle=60, hjust=1),
        axis.ticks.x=element_blank())+
  theme(axis.title.y=element_text(face="bold"),
        axis.text.y = element_text(face="bold", color="black", 
                                   size=12, angle=45))+
  scale_y_continuous(name="Ln chemical fertilizer")+
  scale_x_continuous(name="Year", limits = c(1961, 2021)) 
p1

###Plot manure series
p2 <- ggplot(data = fert.comb, 
             aes(x = year, y = log(man_fert) ))+
  geom_line(color = "brown", size = 1)+
  # geom_smooth(color = "#00AFBB", size = 1)+
  ggtitle( "Manure fertilizer used")+ 
  theme(plot.title = element_text(size=12,hjust = 0.5,face="bold"))+ 
  theme(axis.title.x=element_text(face="bold"),
        axis.text.x=element_text(color="black", 
                                 size=8, angle=60, hjust=1),
        axis.ticks.x=element_blank())+
  theme(axis.title.y=element_text(face="bold"),
        axis.text.y = element_text(face="bold", color="black", 
                                   size=12, angle=45))+
  scale_y_continuous(name="Ln manure-based fertilizer")+
  scale_x_continuous(name="Year", limits = c(1961, 2021)) 
p2



###The Multiplot function
multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) {
  library(grid)
  
  # Make a list from the ... arguments and plotlist
  plots <- c(list(...), plotlist)
  
  numPlots = length(plots)
  
  # If layout is NULL, then use 'cols' to determine layout
  if (is.null(layout)) {
    # Make the panel
    # ncol: Number of columns of plots
    # nrow: Number of rows needed, calculated from # of cols
    layout <- matrix(seq(1, cols * ceiling(numPlots/cols)),
                     ncol = cols, nrow = ceiling(numPlots/cols))
  }
  
  if (numPlots==1) {
    print(plots[[1]])
    
  } else {
    # Set up the page
    grid.newpage()
    pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))
    
    # Make each plot, in the correct location
    for (i in 1:numPlots) {
      # Get the i,j matrix positions of the regions that contain this subplot
      matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE))
      
      print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row,
                                      layout.pos.col = matchidx$col))
    }
  }
}



##Plot the final figure
#All
jpeg("chemswithfull.jpeg", width = 8, height = 6, units = 'in', res = 500)
multiplot(p1,p2, cols=1)
dev.off()
